﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using School.Web.MessageHandler;
using Senparc.Weixin;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.MvcExtension;

namespace School.Web.Controllers
{
    public class WeiXinController : Controller
    {
        private static readonly string Token =
            System.Configuration.ConfigurationManager.AppSettings["Token"];

        [HttpGet]
        [ActionName("Index")]
        public ActionResult Get(string signature, string timestamp, string nonce, string echostr)
        {
            if (CheckSignature.Check(signature, timestamp, nonce, echostr))
            {
                return Content(echostr);
            }
            else
            {
                return Content("failed:" + signature + "," + CheckSignature.GetSignature(timestamp, nonce, Token));
            }
        }

        [HttpPost]
        [ActionName("Index")]
        public ActionResult Post(string signature, string timestamp, string nonce, string echostr)
        {
            if (!CheckSignature.Check(signature, timestamp, nonce, Token))
            {
                return Content("参数错误！");
            }
            //接收消息

            //接收消息
            var messageHandler = new CustomMessageHandler(Request.InputStream);
            messageHandler.Execute();
            return new WeixinResult(messageHandler);
        }

    }
}
